home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / BG_SRC.ZIP / ENCODER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-14  |  5.5 KB  |  201 lines

  1. /*
  2.  * ENCODER.C, encodes plain text so that it can be hidden in the
  3.  * data part of my exe programs, e.g. BG.EXE.
  4.  *
  5.  * This version 8th June 1992
  6.  */
  7.  
  8.  
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <time.h>
  12. #include <malloc.h>
  13. #include <string.h>
  14.  
  15. typedef unsigned char uchar ;
  16. typedef unsigned short ushort ;
  17.  
  18. #include "DEC.H"   /* Contains shifts and masks for encode-decode */
  19.  
  20. #define TRUE  1
  21. #define FALSE 0
  22. #define MAX_CHARS 10000
  23. #define NUL (uchar)0
  24.  
  25. /************************************************************************/
  26.  
  27. uchar* Read_In_File (uchar* In_Name) ;
  28. int    Convert_Buffer (ushort* Cod_Buf, uchar* Str_Buf) ;
  29. void   Write_Cod (ushort* Cod_Buf, uchar* Out_Name) ;
  30.  
  31. /***************************************************************************/
  32.  
  33. void main (int argc, uchar* argv[])
  34. {
  35.     uchar* Str_Buf ;
  36.     ushort* Cod_Buf ;
  37.     ushort Cod_Buf_Len ;
  38.  
  39.     if (argc != 3) {
  40.         printf ("\nUSAGE:\nencoder infile outfile\n\n (add any extensions you need)\n") ;
  41.         exit (1) ;
  42.     }
  43.  
  44.     Str_Buf = Read_In_File (argv[1]) ;
  45.  
  46.     if (Str_Buf != NULL) {
  47.         printf ("\nStr_Buf has %d + 1 chars",strlen (Str_Buf)) ;
  48.         Cod_Buf_Len = (strlen (Str_Buf) + 1) * 2 ; /* uchars --> ushorts */
  49.         printf ("\nWhich means %d bytes when used as shorts",Cod_Buf_Len) ;
  50.         Cod_Buf_Len = Cod_Buf_Len + (3*sizeof(ushort)) ;
  51.                       /* count + chksum + 0xFFFF at end */
  52.         printf ("\nAdding checksum, count and terminator = %d ",Cod_Buf_Len) ;
  53.         Cod_Buf = malloc (Cod_Buf_Len) ;
  54.         if (Cod_Buf != NULL) {
  55.             printf ("\nAbout to convert the buffer...") ;
  56.             if (Convert_Buffer (Cod_Buf,Str_Buf)) {
  57.                 printf ("done\nAbout to to file the buffer...") ;
  58.                 Write_Cod (Cod_Buf,argv[2]) ;
  59.             } else {
  60.                 printf ("\nError converting buffer") ;
  61.             }
  62.             free (Cod_Buf) ;
  63.         } else {
  64.             printf ("\nCould not malloc (%d bytes)",Cod_Buf_Len) ;
  65.             exit (1) ;
  66.         }
  67.         free (Str_Buf) ;
  68.     }
  69. }
  70.  
  71. /***************************************************************************/
  72.  
  73. uchar* Read_In_File (uchar* In_Name)
  74. /*
  75. PURPOSE: To read in all the uchars from the file into a mallocced buffer.
  76. */
  77. {
  78.     uchar* Buffer ;
  79.     FILE* In ;
  80.     ushort i,Reading ;
  81.  
  82.     Buffer = malloc (MAX_CHARS+1) ;
  83.     if (Buffer == NULL) {
  84.         printf ("\nCould not malloc to read %s.",In_Name) ;
  85.         return (NULL) ;
  86.     }
  87.  
  88.     In = fopen (In_Name,"rt") ;
  89.     if (In == NULL) {
  90.         printf ("\nCould not open %s to read.",In_Name) ;
  91.         free (Buffer) ;
  92.         return (NULL) ;
  93.     }
  94.  
  95.     i = 0 ;
  96.     Reading = TRUE ;
  97.     do {
  98.         Buffer[i] = fgetc (In) ;
  99.         if (i == MAX_CHARS) {
  100.             Reading = FALSE ;
  101.             printf ("\nWarning, file too long, truncated") ;
  102.         } else if (feof(In)) {
  103.             Reading = FALSE ;
  104.         } else {
  105.             i++ ;
  106.         }
  107.     } while (Reading) ;
  108.  
  109.     Buffer[i] = NUL ;  /* Zero terminate the string */
  110.  
  111.     fclose (In) ;
  112.  
  113.     printf ("\nRead %d uchars from %s",i,In_Name) ;
  114.  
  115.     return (Buffer) ;
  116. }
  117.  
  118. /***************************************************************************/
  119.  
  120. int Convert_Buffer (ushort* Cod_Buf, uchar* Str_Buf)
  121. /*
  122. PURPOSE: To code up the Str_Buf, putting the coded version in Cod_Buf.
  123. FORMAT:  0th ushort is length of decoded string
  124.          1st ushort is check sum of decoded string
  125.          2nd ushort is first uchar coded up...
  126.          nth ushort is (n-2)th uchar coded up
  127.          last ushort is 0xFFFF
  128. */
  129. {
  130.     ushort cc,sc,Chk_Sum ;
  131.  
  132.     cc = 0 ;  /* The first uchar is... */
  133.     sc = 2 ;  /* ...the 3rd ushort. */
  134.     Chk_Sum = 0 ;
  135.  
  136.     while (Str_Buf[cc] != NUL) {
  137.         Chk_Sum = Chk_Sum + (ushort)Str_Buf[cc] ;
  138.         if (Str_Buf[cc] > 127) {
  139.             Cod_Buf[sc] = ((ushort)Str_Buf[cc] << SPECIAL_SHIFT) | SPECIAL_TAG ;
  140.             Cod_Buf[sc] = Cod_Buf[sc] | (rand() & SPECIAL_RND_MSK) ;
  141.         } else {
  142.             Cod_Buf[sc] = ((ushort)Str_Buf[cc] << NORMAL_SHIFT) | NORMAL_TAG ;
  143.             Cod_Buf[sc] = Cod_Buf[sc] | (rand() & NORMAL_RND_MSK) ;
  144.         }
  145.         cc++ ;
  146.         sc++ ;
  147.     }
  148.     Cod_Buf [sc]    = SHORT_END ;
  149.     Cod_Buf [CHK_I] = Chk_Sum ;
  150.     Cod_Buf [LEN_I] = cc ;
  151.  
  152.     return (TRUE) ;
  153. }
  154.  
  155. /***************************************************************************/
  156.  
  157. void Write_Cod (ushort* Cod_Buf, uchar* Out_Name)
  158. {
  159.     FILE* Out ;
  160.     ushort i ;
  161.  
  162.     Out = fopen (Out_Name,"wt") ;
  163.     if (Out == NULL) {
  164.         printf ("\nCould not open %s to write",Out_Name) ;
  165.         return ;
  166.     }
  167.  
  168.     i = 0 ;
  169.  
  170.     /* Write the checksum and length */
  171.     fprintf (Out,"\n#define %s_CHECK_SUM  0x%X\n",Out_Name,Cod_Buf[CHK_I]) ;
  172.     fprintf (Out,"\n#define %s_LENGTH     %d\n",Out_Name,Cod_Buf[LEN_I]) ;
  173.  
  174.     i = FIRST_CHAR_I ;
  175.     fprintf (Out,"\nushort %s[] = {\n    ",Out_Name) ;
  176.  
  177.     printf ("\n") ;
  178.  
  179.     while (Cod_Buf[i] != SHORT_END) {
  180.         if (!(i%16)) {
  181.             printf ("\r %5d chars written",i) ;
  182.         }
  183.         fprintf (Out,"0x%.4X, ",Cod_Buf[i]) ;
  184.         i++ ;
  185.         if (!((i-FIRST_CHAR_I) % 8)) {
  186.             fprintf (Out,"\n    ") ;
  187.         }
  188.         if (i > MAX_CHARS) {
  189.             printf ("\nWARNING: buffer overflow!") ;
  190.             break ;
  191.         }
  192.     }
  193.  
  194.     fprintf (Out,"0x%.4X};\n",Cod_Buf[i]) ;
  195.     fclose (Out) ;
  196.     printf ("\n") ;
  197.  
  198. }
  199.  
  200. /***************************************************************************/
  201.